//
//  HWTitleTextField.m
//  HWTitleTextFieldDemo
//
//  Created by LiTengFei on 15/3/2.
//  Copyright (c) 2015年 LiTengFei. All rights reserved.
//

#import "HWTitleTextField.h"

static CGFloat title_default_width = 100;

@implementation HWTitleTextField

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){

        self.titleLabel = [[UILabel alloc]init];
        self.titleEdgeInsets = UIEdgeInsetsZero;

        self.textField = [[UITextField alloc]init];
        self.textField.userInteractionEnabled  = YES;
        self.textEdgeInsets = UIEdgeInsetsZero;
        self.textField.textAlignment = NSTextAlignmentRight;
        self.textField.textColor = [UIColor lightGrayColor];
        self.titleWidth = title_default_width;
        
        [self addSubview:_titleLabel];
        [self addSubview:_textField];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];

    NSString * title = self.titleLabel.text;

    if(title == nil){
        title = @"";
    }

    self.titleLabel.frame = CGRectMake(10, 0, self.titleWidth, CGRectGetHeight(self.frame));


    self.textField.frame = CGRectMake(CGRectGetMaxX(self.titleLabel.frame), 0, CGRectGetWidth(self.frame) - CGRectGetMaxX(self.titleLabel.frame) - 10, CGRectGetHeight(self.frame));

//    self.titleLabel.center = CGPointMake(CGRectGetMidX(self.titleLabel.frame), CGRectGetMidY(self.frame) - CGRectGetMinY(self.frame));
//    
//    self.textField.center = CGPointMake(CGRectGetMidX(self.textField.frame), CGRectGetMidY(self.frame) - CGRectGetMinY(self.frame));
}

-(CALayer *)bottomLineLayer{
    if(_bottomLineLayer==nil){
        _bottomLineLayer = [[CALayer alloc]init];
        [self.layer addSublayer:_bottomLineLayer];
        _bottomLineLayer.frame = CGRectMake(0, CGRectGetHeight(self.frame) - 0.5, CGRectGetWidth(self.frame), 0.5);
    }
    return _bottomLineLayer;
}
-(CALayer *)topLineLayer{
    if(_topLineLayer==nil){
        _topLineLayer = [[CALayer alloc]init];
        [self.layer addSublayer:_topLineLayer];
        _topLineLayer.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), 0.5);
    }
    return _topLineLayer;
}
@end
